//
//  SelectFriendView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/20.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "SelectFriendView.h"
#import "SelectFriendCell.h"

@interface SelectFriendView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *list_table_view;

@end



@implementation SelectFriendView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeSelectFriendView];
        
    }
    return self;
}

- (void)makeSelectFriendView
{
    _list_table_view = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _list_table_view.dataSource = self;
    _list_table_view.delegate = self;
    _list_table_view.layer.masksToBounds = YES;
    _list_table_view.layer.borderWidth = 1.0;
    _list_table_view.layer.borderColor = [RGB(240, 240, 240) CGColor];
    _list_table_view.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_list_table_view];
    [_list_table_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-40);
    }];
    
    _list_table_view.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _list_table_view.tableFooterView = nil;
        if ([self.delegate respondsToSelector:@selector(headerWithRefreshingData)]) {
            [self.delegate headerWithRefreshingData];
        }
    }];
    
    [_list_table_view.mj_header beginRefreshing];
}

- (void)setListData:(NSArray *)listData
{
    _listData = listData;
    if (_listData.count == 0) {
        _list_table_view.tableFooterView = [[YJDataStateView sharedStateView] showStateWithTitle:@"暂无好友数据"];
    }
    [_list_table_view reloadData];
    [_list_table_view.mj_header endRefreshing];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SelectFriendCell *listCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SelectFriendCell class])];
    if (!listCell) {
        listCell = [[SelectFriendCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([SelectFriendCell class])];
    }
    
    __weak __typeof(self) weakSelf = self;
    listCell.selectData = ^(NSInteger index){
        [weakSelf didSelectFriendAtIndex:index];
    };
    
    [listCell loadFriendData:(FriendModel *)[_listData objectAtIndex:indexPath.row] index:indexPath.row];
    
    return listCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48;
}

- (void)didSelectFriendAtIndex:(NSInteger)index
{
    FriendModel *model = (FriendModel *)[_listData objectAtIndex:index];
    if ([self.delegate respondsToSelector:@selector(didSelectFriend:)]) {
        [self.delegate didSelectFriend:model];
    }
}


@end
